﻿using System.Collections.Generic;

namespace DevOnServer
{
    public class CCharCharacter
    {
        public int ID;

        public string Name;

        public byte Place;

        public byte Sex;

        public int Job;

        public byte Hair;

        public int HairColor;

        public byte Head;

        public int FOR;

        public int END;

        public int INT;

        public int DEX;

        public int Level;

        public List<int> Equipment = new List<int>();
    }

    public partial class CDatabase
    {
        public void loadCharacters(CCharPlayer player)
        {
            CQueryResult result = m_dbc.result(
                "SELECT id, name, place, sex, job, hair, hairColor, head, stat_FOR, stat_END, stat_DEX, stat_INT, level FROM characters WHERE account='{0}';",
                player.Account.ID);

            CCharCharacter character;
            if (result != null)
            {
                while (result.advance())
                {
                    character = new CCharCharacter();

                    character.ID = result.read<int>("id");
                    character.Name = result.read<string>("name");
                    character.Place = (byte)result.read<int>("place");
                    character.Sex = (byte)result.read<int>("sex");
                    character.Job = result.read<int>("job");
                    character.Hair = (byte)result.read<int>("hair");
                    character.HairColor = result.read<int>("hairColor");
                    character.Head = (byte)result.read<int>("head");
                    character.FOR = result.read<int>("stat_FOR");
                    character.END = result.read<int>("stat_END");
                    character.DEX = result.read<int>("stat_DEX");
                    character.INT = result.read<int>("stat_INT");
                    character.Level = result.read<int>("level");

                    player.Characters[character.Place] = character;
                }

                result.Dispose();
            }

            for (int i = 0; i < 3; i++)
            {
                character = player.Characters[i];

                if (character != null)
                {
                    result = m_dbc.result(
                        "SELECT itemid FROM items WHERE characterid='{0}' and type='{1}' and place>'41' ORDER BY place ASC;",
                        character.ID, (int)ItemType.Inventory);

                    if (result != null)
                    {
                        while (result.advance())
                        {
                            character.Equipment.Add(result.read<int>("itemid"));
                        }

                        result.Dispose();
                    }
                }
            }
        }

        public void deleteCharacter(CCharPlayer player, int charId)
        {
            for (int i = 0; i < 3; i++)
            {
                if (player.Characters[i] != null && player.Characters[i].ID == charId)
                {
                    if (m_dbc.query("UPDATE characters SET account='{0}' WHERE id='{1}' LIMIT 1;",
                        -(player.Account.ID), charId))
                    {
                        player.Characters[i] = null;
                    }

                    return;
                }
            }
        }

        public bool characterExists(string name)
        {
            CQueryResult result = m_dbc.result(
                "SELECT id FROM characters WHERE name='{0}';",
                name);

            if (result != null)
            {
                bool ok = result.advance();
                result.Dispose();
                return ok;
            }

            return false;
        }

        public CCharCharacter createCharacter(CCharPlayer player, byte place, string name, byte hair, int hairColor, byte sex, byte head, int bankpass)
        {
            if (!m_dbc.query("INSERT INTO characters(account, name, place, sex, hair, hairColor, head, bankpass) VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');",
                player.Account.ID, name, place, sex, hair, hairColor, head, bankpass))
                return null;

            CQueryResult result = m_dbc.result("SELECT id, job, stat_FOR, stat_END, stat_DEX, stat_INT, level FROM characters WHERE account='{0}' AND place='{1}' LIMIT 1;",
                player.Account.ID, place);

            if (result != null)
            {
                if (result.advance())
                {
                    CCharCharacter character = player.Characters[place] = new CCharCharacter();

                    character.ID = result.read<int>("id");
                    character.Name = name;
                    character.Place = place;
                    character.Sex = sex;
                    character.Job = result.read<int>("job");
                    character.Head = head;
                    character.Hair = hair;
                    character.HairColor = hairColor;
                    character.FOR = result.read<int>("stat_FOR");
                    character.END = result.read<int>("stat_END");
                    character.DEX = result.read<int>("stat_DEX");
                    character.INT = result.read<int>("stat_INT");
                    character.Level = result.read<int>("level");

                    result.Dispose();

                    return character;
                }

                result.Dispose();
            }

            return null;
        }
    }
}